/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hiddenwebsearch;
import java.util.*;
import java.io.*;
/**
 *
 * @author zhang
 */
public class DBsummary {
    private QProbe q;
    //Initial the QProbe to get DB information
    public DBsummary(QProbe q)
    {this.q=q;}
   
    //get words from url and put the word into summary 
    private HashMap getDBsummary(List<String> url_list)
    {
        String word;
        HashMap<String, Integer> summary = new HashMap<String, Integer>();
        for (int i=0; i<url_list.size(); i++)
        {
            Set result = getWordsLynx.runLynx(url_list.get(i));
            System.out.println("Processing "+"URL: "+url_list.get(i) +"  ("+(i+1)+"/"+url_list.size()+")");
            Iterator iterator = result.iterator();
            while(iterator.hasNext())
            {
                word = iterator.next().toString();
                if (summary.get(word)==null)
                {
                    summary.put(word, 1);
                }
                else
                {
                    int temp = Integer.parseInt(summary.get(word).toString());
                    temp++;
                    summary.put(word, temp);
                }
            }
        }
        return getSortedMap(summary);
    }
    
    //Generate summary from a result node
    public void GenSummary (CNode node)
    {
        List<String> url_list = new ArrayList<String>();
        HashMap URL = new HashMap();
        HashMap result = new HashMap();
        String filename;
        //output root-sample
        System.out.println("\nProcessing Root:");
        URL = AddURL(node.Keyword);
        url_list.addAll(Map_to_List(URL));
        result = getDBsummary(url_list);
        filename = "Sample-" + node.value + "-" + q.DBUrl;
        WriteResult(result, filename);
        
        if (node.Children != null) {
            for (int i = 0; i < node.Children.size(); i++) {
                url_list.clear();
                System.out.println("Porcessing " + node.Children.get(i).value + ":");
                URL = AddURL(node.Children.get(i).Keyword);
                url_list.addAll(Map_to_List(URL));
                result = getDBsummary(url_list);
                filename = "Sample-" + node.Children.get(i).value + "-" + q.DBUrl;
                WriteResult(result, filename);
            }
        }
    }
  
    //Get url and eliminate the duplicate url
    private HashMap AddURL(List<String> qlist)  
    {
        
        HashMap URLlist = new HashMap();
        
        Iterator i = qlist.iterator();
        while(i.hasNext())
        {
            String temp = i.next().toString();
            
            List<String> url_sublist = q.getURL(temp);
            int counter = 0;
          
            while((counter < 4)&&(counter<url_sublist.size()))
            {
                if(URLlist.get(url_sublist.get(counter))==null)
                    URLlist.put(url_sublist.get(counter), 1);
                counter++;
            }
        }
        return URLlist;
    }
    
    //get Url from HashMap and store in List
    private List<String> Map_to_List(HashMap hmap)
    {
        List<String> url_list = new ArrayList<String>();
        Set data = hmap.entrySet();
        Iterator i = data.iterator();
        while(i.hasNext())
        {
            Map.Entry me = (Map.Entry)i.next();
            url_list.add(me.getKey().toString());
        }
        return url_list;
    }
    
    //Use TreeMap to sort the words
    public static HashMap getSortedMap(HashMap<String, Integer> hmap)
    {
        TreeMap map = new TreeMap(hmap);
        HashMap SortedMap = new LinkedHashMap();
        Set data = map.entrySet();
        Iterator i = data.iterator();
        while(i.hasNext())
        {
            Map.Entry me = (Map.Entry)i.next();
            SortedMap.put(me.getKey(), me.getValue());
        }
        
        return SortedMap;
    }
    
    //Write the result into output file
    public static void WriteResult(HashMap result, String filename)
    {
        Set data = result.entrySet();
        Iterator i = data.iterator();
        try{
        File file = new File(filename);
            if(!(file.exists()))
            {
                file.createNewFile();
            }
            BufferedWriter output = new BufferedWriter(new FileWriter(file));
        while(i.hasNext())
        {
            Map.Entry me = (Map.Entry)i.next();
            output.write(me.getKey()+"#"+me.getValue());
            output.write("\n");
        }
       
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
